package Lee_code;

import java.util.Arrays;

public class Lee_18 {
    static class Solution {
        public String longestCommonPrefix(String[] strs)
        {
            StringBuilder sb = new StringBuilder();
            int min_len=99999999;
            for(String s1: strs)
            {
                min_len = Math.min(s1.length(), min_len);
            }
            int[] a = new int[min_len];
            Arrays.fill(a, 1);
            for(int i=0; i<min_len; i++)
            {
                for(int j=1; j<strs.length; j++)
                {
                    if(strs[0].charAt(i)!=strs[j].charAt(i))
                    {
                        a[i]=0;
                    }
                }
            }
            for(int i=0; i<min_len; i++)
            {
                if(a[i]==1)
                {
                    sb.append(strs[0].charAt(i));
                }
                else if (a[i]==0)
                {
                    break;
                }
            }
            String re = sb.toString();
            return re;
        }
    }
    static public void main(String[] args) {
        String[] test = {"flower", "flow", "flight"};
        Solution so = new Solution();
        //System.out.print(666);
        System.out.print(so.longestCommonPrefix(test));

    }
}

